

***************************************
* Table 8: BARGAINING ANALYSIS: ELASTICITIES AND BOOTSTRAPPING STANDARD ERRORS 
***************************************
* Set up data
use  "data_acute_illness_patients_mixedlogit_vs4.dta", clear
	 
* recode typeprovider from 1 to 4 (self=1 , public =2 private =3 doctors=4)
rename typeprovider typeold
generate typeprovider = 1 if typeold==0
replace  typeprovider = 2 if typeold==1
replace  typeprovider = 3 if typeold==2
replace  typeprovider = 4 if typeold==3

* preliminary: gets the starting value and convergence
  set seed 545415
  mixlogit d_  dist_dw_hf_2 dist_22   nu_8d alt2-alt4  , group(choicesit) id(idid) rand( doctor_tot) 
  matrix b50 = e(b)

********************************************************************
* BASELINE MODEL  with barganing interaction on ADULT SAMPLE
*********************************************************************
* Insert the models you focus on: weeklydifflargep50 or agedifflargep50
	 local var_bargain = agedifflargep50
	 *local var_bargain = weeklydifflargep50
	 
 	 set seed 6235482

	 *gen p_ = p_
	 gen p_lowbargain  = p_*`var_bargain'
	 gen p_female = p_*female
	 gen p_female_lowbargain = p_*female*`var_bargain'
	 	  
    * consider only the sample of spouses
	gen spouse = (relation_head==1 | relation_head==2)
	tab relation_head spouse
	
	 matrix b55g = b50[1,1..6], 0.0,  0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0,    0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0 
	 
	 mixlogit d_  dist_dw_hf_2 dist_22   nu_8d alt2-alt4 nu_8dsq if spouse==1 , group(choicesit) id(idid) cluster(q01_10)  rand(doctor_tot  generator2009 beds p_  p_lowbargain p_female p_female_lowbargain ) from(b55g, copy)
	  	   
	 mixlpred PR00, nrep(500)


	 gen esample=1 if e(sample)
	 keep if e(sample)

	 save "tempdata_mixedlogit_bargain_vs2.dta", replace
	 
*/	 
****************************************************************
 * CALCULATE ELASTICITIES OF PRICE BY BARGANING STATUS (SPOUSES)
 ****************************************************************
*keep if spouse==1
keep if e(sample)

* for each type of provider increase the price (for everybody) by 1%
forvalues i=1/4 {

preserve


set seed  246885
drop if idid==1242 /* we drop this individual because it gives implausible value of own price elasticities, positive and in the range of 118*/

*list typeprov female p_i1 p_i2 p_i3 p_i4  p_i1_fem p_i2_fem p_i3_fem p_i4_fem in 1/3 //*to check changes are correct//*
*list typeprov female p_i1 p_i2 p_i3 p_i4  p_i1_fem p_i2_fem p_i3_fem p_i4_fem in 24/26 //*to check changes are correct//*

replace p_                   = p_*(1.01) if typeprov==`i' 
replace p_lowbargain 		 = p_lowbargain*(1.01) if typeprov==`i'
replace p_female      		 = p_female*(1.01) if typeprov==`i' 
replace p_female_lowbargain  = p_female_lowbargain*(1.01) if typeprov==`i'

*replace p_female = p_female*(1.01) if typeprov==`i'
*replace p_male = p_male*(1.01) if typeprov==`i'

list typeprov p_ p_lowbargain p_female    p_female_lowbargain female  in 1/3  //*to check changes are correct//*
list typeprov p_ p_lowbargain p_female    p_female_lowbargain female in 24/26 //*to check changes are correct//*

mixlpred PR1`i', nrep(500)

gen el_p`i' = 100*(PR1`i'-PR00)/PR00

*drop if idid== 1242 /* we drop this observation because have outlier elasticities' values are outlier, values over 118! */

display "THESE ARE OWN AND CROSS PRICE ELASTICiTIES FOR 1% INCREASE IN PRICE IN TYPEPROVIDER= `i' "
*bysort quartile typeprov: sum el_p`i'
table typeprov female, c(mean el_p`i' med el_p`i' N el_p`i') format(%9.3f) 

table typeprov female agedifflarge, c(mean el_p`i' med el_p`i' N el_p`i') format(%9.3f) col row

display "*HIGH* BARGAINING HOUSEHOLDS: THESE ARE OWN AND CROSS PRICE ELASTICiTIES FOR 1% INCREASE IN PRICE IN TYPEPROVIDER= `i' "
table typeprov female if agedifflarge==0, c(mean el_p`i' med el_p`i' N el_p`i') format(%9.3f) col row
display "*LOW* BARGAINING HOUSEHOLDS: THESE ARE OWN AND CROSS PRICE ELASTICiTIES FOR 1% INCREASE IN PRICE IN TYPEPROVIDER= `i' "
table typeprov female if agedifflarge==1, c(mean el_p`i' med el_p`i' N el_p`i') format(%9.3f) col row

restore 
}



*********************************************************************************************************
 * BOOTSTRAPPING DIFFERENCES IN ELASTICITIES OF PRICE BY BARGAINING POWER STATUS and BY GENDER (SAMPLE OF SPOUSES)
*********************************************************************************************************
** This do file estimates the specification of RPM model with the price coefficients varying by bargaining status
** It bootstraps price elasticities by changing the price by 1% for each provider in turn, and then tabulates the price elasticities of demand by barganing power and by gender
** It saves the simulated elasticities in a data file mixboot_bargain_vs3.dta which is used to compute the elastciities s.e. in Table 8

set seed 246885
local bsize=100 /* size of the iterations*/
global regressors "dist_dw_hf_2 dist_22   nu_8d alt2 alt3 alt4 nu_8dsq "
global random "doctor_tot  generator2009 beds p_  p_lowbargain p_female p_female_lowbargain"
global control "idid choicesit quartile no_facility i1 i2 i3 i4  age age2 typeprov p_ female agedifflarge relation_head spouse children" 
set more off
tempname 3
postfile `3' iter   el_p11mh el_p12mh el_p13mh el_p14mh  /// *self for male, high bargain*
					el_p21mh el_p22mh el_p23mh el_p24mh  /// *public*
					el_p31mh el_p32mh el_p33mh el_p34mh  /// *private*
					el_p41mh el_p42mh el_p43mh el_p44mh  /// *doctor*
					el_p11fh el_p12fh el_p13fh el_p14fh  /// *self for female, high bargain *
					el_p21fh el_p22fh el_p23fh el_p24fh  /// *public*
					el_p31fh el_p32fh el_p33fh el_p34fh  /// *private*
					el_p41fh el_p42fh el_p43fh el_p44fh  /// *doctor*
					///
					el_p11ml el_p12ml el_p13ml el_p14ml  /// *self for male, low baragin*
					el_p21ml el_p22ml el_p23ml el_p24ml  /// *public*
					el_p31ml el_p32ml el_p33ml el_p34ml  /// *private*
					el_p41ml el_p42ml el_p43ml el_p44ml  /// *doctor*
					el_p11fl el_p12fl el_p13fl el_p14fl  /// *self for female, high bargain*
					el_p21fl el_p22fl el_p23fl el_p24fl  /// *public*
					el_p31fl el_p32fl el_p33fl el_p34fl  /// *private*
					el_p41fl el_p42fl el_p43fl el_p44fl  /// *doctor*
					///
					del_ph11 del_ph12 del_ph13 del_ph14  /// difference by gender, high bargain
					del_ph21 del_ph22 del_ph23 del_ph24  ///
					del_ph31 del_ph32 del_ph33 del_ph34  ///
					del_ph41 del_ph42 del_ph43 del_ph44  ///
					///
					del_pl11 del_pl12 del_pl13 del_pl14  /// difference by gender, low bargain
					del_pl21 del_pl22 del_pl23 del_pl24  ///
					del_pl31 del_pl32 del_pl33 del_pl34  ///
					del_pl41 del_pl42 del_pl43 del_pl44  ///
                    ///
					del_pf11 del_pf12 del_pf13 del_pf14  /// difference by bargain, female
					del_pf21 del_pf22 del_pf23 del_pf24  ///
					del_pf31 del_pf32 del_pf33 del_pf34  ///
					del_pf41 del_pf42 del_pf43 del_pf44  ///
                    ///
					del_pm11 del_pm12 del_pm13 del_pm14  /// difference by bargain, male
					del_pm21 del_pm22 del_pm23 del_pm24  ///
					del_pm31 del_pm32 del_pm33 del_pm34  ///
					del_pm41 del_pm42 del_pm43 del_pm44  ///
                    ///
					using mixboot_bargain_vs3, replace   
forvalues iters1=1(1)`bsize' {
use "tempdata_mixedlogit_bargain_vs2.dta",  clear
keep if esample==1
*keep if e(sample)
drop if idid ==1242
keep idid no_facility d_ $control $regressors $random
bsample 592, cluster(idid) idcluster(idnew)
replace choicesit =idnew
				noisily di "  ITER =" "`iters1'" "ESTIMATION OF RANDOM PARAMETER MODEL "
				* estimate RPM for bootstrapped sample 'iter'

			    mixlogit d_  $regressors  if spouse==1 , group(choicesit) id(idnew) rand($random) from(b55g, copy)

	            mixlpred PR00, nrep(500)
	          
* CALCULATE  THE SIMULATED ELASTICITY BY CHANGING X BY 1%
* for each type of provider increase the price (for everybody) by 1%
forvalues i=1/4 {
noisily di "WE ARE GOING TO CHANGE PRICE FOR PROVIDER=" "`i'" 

local i=`i'     //*typeprovider whose price is being changed*//
preserve

replace p_                   = p_*(1.01)                  if typeprov==`i' 
replace p_lowbargain 		 = p_lowbargain*(1.01)        if typeprov==`i'
replace p_female      		 = p_female*(1.01)            if typeprov==`i' 
replace p_female_lowbargain  = p_female_lowbargain*(1.01) if typeprov==`i'

mixlpred PR1`i', nrep(500)

gen el_p`i' = 100*(PR1`i'-PR00)/PR0

* own and cross elasticities: for each provider (whose price changes), store own elasticity and cross
* if price changes in i, what happen to # => E#z(i)

noisily di "===> OWN AND CROSS ELASTICITY FOR MALE IN HIGH BARGAINING HH: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1 & female==0  & agedifflarge==0 
local el_p`i'1mh = r(mean)
sum el_p`i' if typeprov==2 & female==0  & agedifflarge==0 
local el_p`i'2mh = r(mean)
sum el_p`i' if typeprov==3 & female==0  & agedifflarge==0 
local el_p`i'3mh = r(mean)
sum el_p`i' if typeprov==4 & female==0  & agedifflarge==0 
local el_p`i'4mh = r(mean)


noisily di "===> OWN AND CROSS ELASTICITY FOR FEMALE IN HIGH BARGAINING HH: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1 & female==1  & agedifflarge==0 
local el_p`i'1fh = r(mean)
sum el_p`i' if typeprov==2 & female==1  & agedifflarge==0 
local el_p`i'2fh = r(mean)
sum el_p`i' if typeprov==3 & female==1  & agedifflarge==0 
local el_p`i'3fh = r(mean)
sum el_p`i' if typeprov==4 & female==1  & agedifflarge==0 
local el_p`i'4fh = r(mean)

noisily di "===> OWN AND CROSS ELASTICITY FOR MALE IN LOW BARGAINING HH: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1 & female==0  & agedifflarge==1 
local el_p`i'1ml = r(mean)
sum el_p`i' if typeprov==2 & female==0  & agedifflarge==1
local el_p`i'2ml = r(mean)
sum el_p`i' if typeprov==3 & female==0  & agedifflarge==1 
local el_p`i'3ml = r(mean)
sum el_p`i' if typeprov==4 & female==0  & agedifflarge==1 
local el_p`i'4ml = r(mean)
		  
			  
noisily di "===> OWN AND CROSS ELASTICITY FOR FEMALE IN LOW BARGAINING HH: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1 & female==1  & agedifflarge==1 
local el_p`i'1fl = r(mean)
sum el_p`i' if typeprov==2 & female==1  & agedifflarge==1
local el_p`i'2fl = r(mean)
sum el_p`i' if typeprov==3 & female==1  & agedifflarge==1 
local el_p`i'3fl = r(mean)
sum el_p`i' if typeprov==4 & female==1  & agedifflarge==1 
local el_p`i'4fl = r(mean)
		  


noisily di "===> GET THE DIFFERENCE IN PRICE ELASITICITIES FEMALE-MALE (HIGH BARGAIN) FOR A 1% PRICE CHANGE IN PROVIDER=" "`i'" 

ttest el_p`i' if typeprov==1 & agedifflarge==0, by(female)  // mean(1)-mean(0)= mean(female)-mean(male)
local del_ph`i'1 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==2 & agedifflarge==0, by(female)
local del_ph`i'2 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==3 & agedifflarge==0, by(female)
local del_ph`i'3 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==4 & agedifflarge==0, by(female)
local del_ph`i'4 = r(mu_2)-r(mu_1)

noisily di "===> GET THE DIFFERENCE IN PRICE ELASITICITIES FEMALE-MALE (LOW BARGAIN) FOR A 1% PRICE CHANGE IN PROVIDER=" "`i'" 

ttest el_p`i' if typeprov==1 & agedifflarge==1, by(female)   // mean(1)-mean(0)= mean(female)-mean(male)
local del_pl`i'1 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==2 & agedifflarge==1, by(female)
local del_pl`i'2 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==3 & agedifflarge==1, by(female)
local del_pl`i'3 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==4 & agedifflarge==1, by(female)
local del_pl`i'4 = r(mu_2)-r(mu_1)

noisily di "===> GET THE DIFFERENCE IN PRICE ELASITICITIES HIGH-LOW BARGAIIN POWER IN FEMALE FOR A 1% PRICE CHANGE IN PROVIDER=" "`i'" 

ttest el_p`i' if typeprov==1 & female==1, by(agedifflarge)    // mean(1)-mean(0)= mean(low bargain)-mean(high bargain)
local del_pf`i'1 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==2 & female==1, by(agedifflarge)
local del_pf`i'2 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==3 & female==1, by(agedifflarge)
local del_pf`i'3 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==4 & female==1, by(agedifflarge)
local del_pf`i'4 = r(mu_2)-r(mu_1)

noisily di "===> GET THE DIFFERENCE IN PRICE ELASITICITIES LOW-HIGH BARGAIIN POWER IN FEMALE FOR A 1% PRICE CHANGE IN PROVIDER=" "`i'" 

ttest el_p`i' if typeprov==1 & female==0, by(agedifflarge)
local del_pm`i'1 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==2 & female==0, by(agedifflarge)
local del_pm`i'2 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==3 & female==0, by(agedifflarge)
local del_pm`i'3 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==4 & female==0, by(agedifflarge)
local del_pm`i'4 = r(mu_2)-r(mu_1)


restore


}

local iter=`iters1'


post `3' (`iter')   (`el_p11mh') (`el_p12mh') (`el_p13mh') (`el_p14mh')  /// *self for male*
					(`el_p21mh') (`el_p22mh') (`el_p23mh') (`el_p24mh')  /// *public*
					(`el_p31mh') (`el_p32mh') (`el_p33mh') (`el_p34mh')  /// *private*
					(`el_p41mh') (`el_p42mh') (`el_p43mh') (`el_p44mh')  /// *doctor*
					///
					(`el_p11fh') (`el_p12fh') (`el_p13fh') (`el_p14fh')  /// *self*
					(`el_p21fh') (`el_p22fh') (`el_p23fh') (`el_p24fh')  /// *public*
					(`el_p31fh') (`el_p32fh') (`el_p33fh') (`el_p34fh')  /// *private*
					(`el_p41fh') (`el_p42fh') (`el_p43fh') (`el_p44fh')  ///
					///
					(`el_p11ml') (`el_p12ml') (`el_p13ml') (`el_p14ml')  /// *self for male*
					(`el_p21ml') (`el_p22ml') (`el_p23ml') (`el_p24ml')  /// *public*
					(`el_p31ml') (`el_p32ml') (`el_p33ml') (`el_p34ml')  /// *private*
					(`el_p41ml') (`el_p42ml') (`el_p43ml') (`el_p44ml')  /// *doctor*
					///
					(`el_p11fl') (`el_p12fl') (`el_p13fl') (`el_p14fl')  /// *self for male*
					(`el_p21fl') (`el_p22fl') (`el_p23fl') (`el_p24fl')  /// *public*
					(`el_p31fl') (`el_p32fl') (`el_p33fl') (`el_p34fl')  /// *private*
					(`el_p41fl') (`el_p42fl') (`el_p43fl') (`el_p44fl')  /// *doctor*
					///
					(`del_ph11') (`del_ph12') (`del_ph13') (`del_ph14')  /// difference in elasticities of male and female in high bargain
					(`del_ph21') (`del_ph22') (`del_ph23') (`del_ph24')  ///
					(`del_ph31') (`del_ph32') (`del_ph33') (`del_ph34')  ///
					(`del_ph41') (`del_ph42') (`del_ph43') (`del_ph44')  ///
					///
					(`del_pl11') (`del_pl12') (`del_pl13') (`del_pl14')  /// difference in elasticities of male and female in low bargain
					(`del_pl21') (`del_pl22') (`del_pl23') (`del_pl24')  ///
					(`del_pl31') (`del_pl32') (`del_pl33') (`del_pl34')  ///
					(`del_pl41') (`del_pl42') (`del_pl43') (`del_pl44')  ///
					///
					(`del_pf11') (`del_pf12') (`del_pf13') (`del_pf14')  /// difference in elasticities of low-high bargain in female
					(`del_pf21') (`del_pf22') (`del_pf23') (`del_pf24')  ///
					(`del_pf31') (`del_pf32') (`del_pf33') (`del_pf34')  ///
					(`del_pf41') (`del_pf42') (`del_pf43') (`del_pf44')  ///
					///
					(`del_pm11') (`del_pm12') (`del_pm13') (`del_pm14')  /// difference in elasticities of low-high bargain in male
					(`del_pm21') (`del_pm22') (`del_pm23') (`del_pm24')  ///
					(`del_pm31') (`del_pm32') (`del_pm33') (`del_pm34')  ///
					(`del_pm41') (`del_pm42') (`del_pm43') (`del_pm44')  
}
postclose `3'
		

		
use "mixboot_bargain_vs3" , clear

sum el*

* To compute the p-value of the difference in elasticities we use a Fisher test approach to p-values.
* If female have more elastic demand, we expect to see difference (female - male) elasticities to be a negative value. 
* How likely it that we obtain an estimated  difference (female - male) than is not negative? 


* one-tailed null hypothesis of difference = zero (or greater than zero)
count if del_ph11 >= 0
count if del_ph22 >= 0
count if del_ph33 >= 0
count if del_ph44 >= 0


* one-tailed null hypothesis of difference = zero (or greater than zero)
count if del_pl11 >= 0
count if del_pl22 >= 0
count if del_pl33 >= 0
count if del_pl44 >= 0



/*
